﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Threading;
using CoreLib;
using CoreLib.BusinessLogic;
using CoreLib.DAL;
using CoreLib.Utils;
using RKMSikraKulingram.PresentationEntities;

namespace RKMSikraKulingram
{
    public partial class DevoteeDetails : Form
    {
        # region Private Data Members

        private bool _modifyingExistingTransactionDetails = false;

        private int _currentPersonID = 0;
        private bool _personIsDirty = false;
        private Person _currentPerson = null;

        private int _currentAddressID = 0;
        private bool _addressIsDirty = false;
        private Address _currentAddress = null;

        private int _currentTransactionID = 0;
        private bool _transactionIsDirty = false;
        private Transaction _currentTransaction = null;

        private int _currentTransactionReceiptDetailID = 0;
        private bool _transactionReceiptDetailIsDirty = false;
        private TransactionReceiptDetail _currentTransactionReceiptDetail = null;

        # endregion

        # region Constructors

        public DevoteeDetails()
        {
            InitializeComponent();
        }

        public DevoteeDetails(int personID, int addressID, int transactionID, int transactionReceiptDetailsID)
            : this()
        {
            _modifyingExistingTransactionDetails = true;
            _currentPersonID = personID;
            _currentAddressID = addressID;
            _currentTransactionID = transactionID;
            _currentTransactionReceiptDetailID = transactionReceiptDetailsID;
        }

        # endregion Constructors

        # region Event Handlers

        void MainForm_Load(object sender, System.EventArgs e)
        {
            initializeDateFields();
            clearErrorTexts(true, true, true);

            this.Owner.Enabled = false;

            if (!_modifyingExistingTransactionDetails)
            {
                Utilities.FormUtilities.ComboBoxPopulation(this.comboBoxFirstName, CommonBLL.CreateCommonBLL().GetPersonFirstNames());
            }
            else
            {
                this.checkBoxNewUser.Checked = false;
                this.checkBoxNewUser.Visible = false; 
            }
            Utilities.FormUtilities.ComboBoxPopulation(this.comboBoxTitle, Constants.constantTitles);
            Utilities.FormUtilities.ComboBoxPopulation(this.comboBoxPaymentMode, CommonBLL.CreateCommonBLL().GetTransactionModeNames());
            Utilities.FormUtilities.ComboBoxPopulation(this.comboBoxPurpose, CommonBLL.CreateCommonBLL().GetDonationPlaceNames());
            Utilities.FormUtilities.ComboBoxPopulation(this.comboBoxSentByType, CommonBLL.CreateCommonBLL().GetPostalModeNames());

            if (_currentPersonID > 0 && _currentAddressID > 0)
                populatePersonDetailsWithExistingUser(_currentPersonID, _currentAddressID);

            if (_currentTransactionID > 0)
                populateTransactionDetailsWithExisting(_currentTransactionID);

            if (_currentTransactionReceiptDetailID > 0)
                populateTransactionReceiptDetailsWithExisting(_currentTransactionReceiptDetailID);
        }

        void DevoteeDetails_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            this.Owner.Enabled = true;
            if (null != DevoteeDetailsClosing)
                DevoteeDetailsClosing(sender, e);
        }

        private void comboBoxTitle_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        void comboBoxFirstName_GotFocus(object sender, System.EventArgs e)
        {
            if (!_modifyingExistingTransactionDetails)
                this.comboBoxFirstName.DroppedDown = true;
        }

        private void checkBoxNewUser_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxNewUser.Checked)
            {
                this.dateTimePickerDate.Enabled = true;
                clearTexts(true, true, true);
                clearErrorTexts(true, true, true);
            }
            else
            {
                this.dateTimePickerDate.Enabled = false;
            }
        }

        void comboBoxFirstName_DropDownClosed(object sender, System.EventArgs e)
        {
            if (this.comboBoxFirstName.SelectedIndex >= 0)
            {
                this.checkBoxNewUser.Checked = false;
                FirstName firstName = this.comboBoxFirstName.SelectedItem as FirstName;
                populatePersonDetailsWithExistingUser(firstName.PersonID, firstName.AddressID);
            }
            else
            {
                this.checkBoxNewUser.Checked = true;
            }
            Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => this.comboBoxFirstName.Capture = false));
        }

        void dateTimePickerDate_GotFocus(object sender, EventArgs e)
        {
            //SendKeys.Send("{RIGHT 0}");
        }

        # region Utilities

        private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Color PenCOlor = Color.FromArgb(93, 93, 93);

            Pen p = new Pen(PenCOlor, 10);

            Point pt1 = new Point(0, this.Height - 100);

            Point pt2 = new Point(100, this.Height - 100);

            e.Graphics.DrawLine(p, pt1, pt2);
        }

        # endregion

        private void comboBoxPaymentMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxPaymentMode.SelectedIndex == 0)
            {
                comboBoxDonationAccount.Visible = false;
                makeModeGridVisible("Mode", false);
                return;
            }
            object o = this.comboBoxPaymentMode.SelectedItem;
            if (o is TransactionModeNames)
            {
                TransactionModeNames modeName = o as TransactionModeNames;
                if (modeName.Name.Equals("Cash"))
                {
                    makePurposeGridVisibleOrInvisible(true);

                    labelTransactionDate.Enabled = true;
                    dateTimePickerTransactionDate.Enabled = true;
                    labelRequiredModeDate.Enabled = true;

                    labelModeNumber.Text = "Mode Number";
                    labelModeNumber.Enabled = false;
                    labelRequiredModeNumber.Enabled = false;
                    textBoxModeNumber.Enabled = false;

                    labelTransactionModeDate.Text = "Mode Date";
                    labelTransactionModeDate.Enabled = false;
                    labelRequiredTransactionModeDate.Enabled = false;
                    dateTimePickerTransactionModeDate.Enabled = false;

                    labelTransactionModeBankName.Enabled = false;
                    labelRequiredTransactionModeBankName.Enabled = false;
                    textBoxTransactionModeBankName.Enabled = false;

                    labelTransactionAmount.Enabled = true;
                    labelRequiredTransactionAmount.Enabled = true;
                    textBoxTransactionAmount.Enabled = true;
                }
                else if (modeName.Name.Equals("Cheque"))
                {
                    makeModeGridVisible("Cheque", true);
                }
                else if (modeName.Name.Equals("DD"))
                {
                    makeModeGridVisible("DD", true);
                }
                else if (modeName.Name.Equals("MO"))
                {
                    makeModeGridVisible("MO", true);
                }
                else if (modeName.Name.Equals("BankTransaction"))
                {
                    makeModeGridVisible("Transaction", true);
                }
                else
                {
                    comboBoxDonationAccount.Visible = false;
                    makeModeGridVisible("Mode", false);
                }
            }
        }

        private void comboBoxPurpose_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxPurpose.SelectedIndex == 0)
            {
                comboBoxDonationAccount.Visible = false;
                return;
            }
            object o = this.comboBoxPurpose.SelectedItem;
            if (o is DonationPlaceNames)
            {
                DonationPlaceNames donationPlaceName = o as DonationPlaceNames;
                DonationAccountNames[] donationAccountNames = CommonBLL.CreateCommonBLL().GetDonationAccountNames(donationPlaceName.DonationPlaceID);
                if (donationAccountNames.Count<DonationAccountNames>() > 0)
                {
                    Utilities.FormUtilities.ComboBoxPopulation(this.comboBoxDonationAccount, donationAccountNames);
                    if (_modifyingExistingTransactionDetails)
                    {
                        makeDonationAccountComboBoxSelection();
                    }
                    this.comboBoxDonationAccount.Visible = true;
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            string message = "Saved successfully: ";
            if (checkPersonDetailsFields())
            {
                if (this.checkBoxNewUser.Checked)
                {
                    Address address = new Address()
                    {
                        AddressLine1 = this.textBoxAddressLine1.Text,
                        AddressLine2 = this.textBoxAddressLine2.Text,
                        AddressLine3 = this.textBoxAddressLine3.Text,
                        AddressLine4 = this.textBoxAddressLine4.Text,
                        PhoneNumber = this.textBoxPhoneNumber.Text,
                        Email = this.textBoxEmailAddress.Text,
                    };

                    CommonBLL.CreateCommonBLL().InsertAddress(ref _currentAddressID, address);

                    Person person = new Person()
                    {
                        //Title = (this.comboBoxTitle.SelectedIndex > 0) ?
                        //this.comboBoxTitle.SelectedItem as string : "",
                        AddressID = _currentAddressID,
                        Title = this.textBoxTitle.Text,
                        FirstName = this.comboBoxFirstName.Text,
                        LastName = this.textBoxLastName.Text,
                        EntryDate = this.dateTimePickerDate.Value
                    };

                    CommonBLL.CreateCommonBLL().InsertPerson(ref _currentPersonID, person);
                    message += " New user";
                }
                else
                {
                    if (_currentPersonID == 0)
                    {
                        MessageBox.Show("Please re-enter the First Name field.", "OK", MessageBoxButtons.OK);
                        return;
                    }

                    _currentAddress.AddressLine1 = this.textBoxAddressLine1.Text;
                    _currentAddress.AddressLine2 = this.textBoxAddressLine2.Text;
                    _currentAddress.AddressLine3 = this.textBoxAddressLine3.Text;
                    _currentAddress.AddressLine4 = this.textBoxAddressLine4.Text;
                    _currentAddress.PhoneNumber = this.textBoxPhoneNumber.Text;
                    _currentAddress.Email = this.textBoxEmailAddress.Text;

                    if (_addressIsDirty)
                    {
                        if (MessageBox.Show(string.Format(
                            "Do you want to update the master address record for {0}?", currentPersonName()),
                            "Yes", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            CommonBLL.CreateCommonBLL().UpdateAddress(_currentAddressID, _currentAddress);
                        }
                        else
                        {
                            CommonBLL.CreateCommonBLL().InsertAddress(ref _currentAddressID, _currentAddress);
                        }
                    }

                    _currentPerson.AddressID = _currentAddressID;
                    //_currentPerson.Title = (this.comboBoxTitle.SelectedIndex > 0) ? this.comboBoxTitle.SelectedItem as string : "";
                    _currentPerson.Title = this.textBoxTitle.Text;
                    _currentPerson.FirstName = this.comboBoxFirstName.Text;
                    _currentPerson.LastName = this.textBoxLastName.Text;
                    _currentPerson.EntryDate = this.dateTimePickerDate.Value;

                    if (_personIsDirty)
                        CommonBLL.CreateCommonBLL().UpdatePerson(_currentPersonID, _currentPerson);
                    message += " Modified user";
                }

                if (this.comboBoxPaymentMode.SelectedIndex > 0)
                {
                    if (checkTransactionDetailsFields())
                    {
                        if (!_modifyingExistingTransactionDetails)
                        {
                            Transaction newTransaction = new Transaction()
                            {
                                PersonID = _currentPersonID,
                                AddressID = _currentAddressID,
                                TransactionModeID = (this.comboBoxPaymentMode.SelectedItem as TransactionModeNames).ModeID,
                                TransactionModeDate = (this.dateTimePickerTransactionModeDate).Value,
                                TransactionDate = (this.dateTimePickerTransactionDate).Value,
                                TransactionModeBank = this.textBoxTransactionModeBankName.Text,
                                TransactionModeNumber = this.textBoxModeNumber.Text,
                                TransactionAmount = decimal.Parse(this.textBoxTransactionAmount.Text)
                            };

                            _currentTransactionID = CommonBLL.CreateCommonBLL().InsertTransaction(newTransaction);
                            message += ", Transaction Detail";
                        }
                        else
                        {
                            if (_currentTransactionID == 0)
                            {
                                MessageBox.Show("Please re-enter the Transaction Detail fields.", "OK", MessageBoxButtons.OK);
                                return;
                            }

                            _currentTransaction.PersonID = _currentPersonID;
                            _currentTransaction.AddressID = _currentAddressID;
                            _currentTransaction.TransactionModeID = (this.comboBoxPaymentMode.SelectedItem as TransactionModeNames).ModeID;
                            _currentTransaction.TransactionModeDate = (this.dateTimePickerTransactionModeDate).Value;
                            _currentTransaction.TransactionDate = (this.dateTimePickerTransactionDate).Value;
                            _currentTransaction.TransactionModeBank = this.textBoxTransactionModeBankName.Text;
                            _currentTransaction.TransactionModeNumber = this.textBoxModeNumber.Text;
                            _currentTransaction.TransactionAmount = decimal.Parse(this.textBoxTransactionAmount.Text);

                            if (_transactionIsDirty)
                            {
                                CommonBLL.CreateCommonBLL().UpdateTransaction(_currentTransactionID, _currentTransaction);
                                message += ", Modified Transaction Detail";
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please re-enter the Transaction Detail fields.", "OK", MessageBoxButtons.OK);
                        return;
                    }
                }

                if (this.comboBoxPurpose.SelectedIndex > 0)
                {
                    if (checkTransactionReceiptDetailsFields())
                    {
                        if (!_modifyingExistingTransactionDetails)
                        {
                            TransactionReceiptDetail newReceiptDetails = new TransactionReceiptDetail()
                            {
                                TransactionID = _currentTransactionID,
                                DonationPlaceID = (this.comboBoxPurpose.SelectedItem as DonationPlaceNames).DonationPlaceID,
                                DonationAccountID = (this.comboBoxDonationAccount.SelectedItem as DonationAccountNames).DonationAccountID,
                                TransactionReceiptNumber = this.textBoxReceiptNumber.Text,
                                TransactionReceiptDate = this.dateTimePickerReceiptDate.Value,
                                PostalModeID = (this.comboBoxSentByType.SelectedIndex > 0) ?
                                (this.comboBoxSentByType.SelectedItem as PostalModeNames).PostalModeID : 0,
                                DespatchDate = this.dateTimePickerDespatchDate.Value
                            };
                            CommonBLL.CreateCommonBLL().InsertReceiptDetails(newReceiptDetails);
                            message += ", Receipt Detail";
                        }
                        else
                        {
                            if (_currentTransactionReceiptDetailID == 0)
                            {
                                MessageBox.Show("Please re-enter the Transaction Receipt Detail fields.", "OK", MessageBoxButtons.OK);
                                return;
                            }
                            _currentTransactionReceiptDetail.TransactionID = _currentTransactionID;
                            _currentTransactionReceiptDetail.DonationPlaceID = (this.comboBoxPurpose.SelectedItem as DonationPlaceNames).DonationPlaceID;
                            _currentTransactionReceiptDetail.DonationAccountID = (this.comboBoxDonationAccount.SelectedItem as DonationAccountNames).DonationAccountID;
                            _currentTransactionReceiptDetail.TransactionReceiptNumber = this.textBoxReceiptNumber.Text;
                            _currentTransactionReceiptDetail.TransactionReceiptDate = this.dateTimePickerReceiptDate.Value;
                            _currentTransactionReceiptDetail.PostalModeID = (this.comboBoxSentByType.SelectedIndex > 0) ?
                            (this.comboBoxSentByType.SelectedItem as PostalModeNames).PostalModeID : 0;
                            _currentTransactionReceiptDetail.DespatchDate = this.dateTimePickerDespatchDate.Value;

                            if (_transactionReceiptDetailIsDirty)
                            {
                                CommonBLL.CreateCommonBLL().UpdateTransactionReceiptDetails(_currentTransactionReceiptDetailID, _currentTransactionReceiptDetail);
                                message += ", Modified Transaction Receipt Detail";
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please re-enter the Transaction Receipt Detail fields.", "OK", MessageBoxButtons.OK);
                        return;
                    }
                }

                MessageBox.Show(message, "Ok", MessageBoxButtons.OK);
                this.Close();
            }
        }

        # endregion Event Handlers

        # region Public Interface

        public event EventHandler DevoteeDetailsClosing;

        # endregion


        # region Private Methods

        private string currentPersonName()
        {
            string personName = "";
            if (!string.IsNullOrEmpty(this.textBoxTitle.Text))
                personName += this.textBoxTitle.Text + " ";
            personName += this.textBoxLastName.Text + " " + this.textBoxLastName.Text;
            return personName;
        }

        private void populatePersonDetailsWithExistingUser(int personID, int addressID)
        {
            _currentPersonID = personID;
            _currentPerson = CommonBLL.CreateCommonBLL().GetPersonByPersonID(_currentPersonID);
            _currentPerson.PropertyChanged += (o, e1) => { _personIsDirty = true; };

            _currentAddressID = addressID;
            _currentAddress = CommonBLL.CreateCommonBLL().GetAddressByAddressID(_currentAddressID);
            _currentAddress.PropertyChanged += (o, e1) => { _addressIsDirty = true; };

            populatePersonDetailsWithExistingUser();
        }

        private void populateTransactionDetailsWithExisting(int transactionID)
        {
            if (transactionID == 0)
                return;

            _currentTransactionID = transactionID;
            _currentTransaction = CommonBLL.CreateCommonBLL().GetTransactionByTransactionID(_currentTransactionID);
            _currentTransaction.PropertyChanged += (o, e) => { _transactionIsDirty = true; };

            populateTransactionDetailsWithExisting();
        }

        private void populateTransactionReceiptDetailsWithExisting(int transactionReceiptDetailsID)
        {
            if (transactionReceiptDetailsID == 0)
                return;

            _currentTransactionReceiptDetailID = transactionReceiptDetailsID;
            _currentTransactionReceiptDetail = CommonBLL.CreateCommonBLL().GetTransactionReceiptDetailByTransactionReceiptDetailID(_currentTransactionReceiptDetailID);
            _currentTransactionReceiptDetail.PropertyChanged += (o, e) => { _transactionReceiptDetailIsDirty = true; };

            populateTransactionReceiptDetailsWithExisting();
        }

        private void initializeDateFields()
        {
            DateTime dt = DateTime.Now;
            this.dateTimePickerDate.Value = dt;
            this.dateTimePickerTransactionDate.Value = dt;
            this.dateTimePickerTransactionModeDate.Value = dt;
            this.dateTimePickerReceiptDate.Value = dt;
            this.dateTimePickerDespatchDate.Value = dt;
        }

        private bool checkPersonDetailsFields()
        {
            bool status = true;

            if (string.IsNullOrEmpty(this.comboBoxFirstName.Text) ||
                string.IsNullOrEmpty(this.textBoxLastName.Text) ||
                string.IsNullOrEmpty(this.textBoxAddressLine1.Text) ||
                (!RegularExpressionMatches.IsAnEmail(this.textBoxEmailAddress.Text,
                this.labelErrorEmailAddress, string.Format(Resources.SikraResource.WrongInputErrorMsg, "an", "email"))))
            {
                status = false;
            }

            if (!status)
            {
                MessageBox.Show("Enter correct Devotee details.", "Ok", MessageBoxButtons.OK);
            }

            return status;
        }

        private bool checkTransactionDetailsFields()
        {
            bool status = true;

            if (!RegularExpressionMatches.IsAnAmount(this.textBoxEmailAddress.Text,
                this.labelErrorAmount, string.Format(Resources.SikraResource.WrongInputErrorMsg, "an", "amount")))
            {
                status = false;
            }

            object o = this.comboBoxPaymentMode.SelectedItem;
            if (o is TransactionModeNames)
            {
                TransactionModeNames modeName = o as TransactionModeNames;
                if (!modeName.Name.Equals("Cash"))
                {
                    if (string.IsNullOrEmpty(this.textBoxModeNumber.Text) ||
                        string.IsNullOrEmpty(this.textBoxTransactionModeBankName.Text))
                    {
                        status = false;
                    }
                }
            }

            if (!status)
            {
                MessageBox.Show("Enter correct Transaction details.", "Ok", MessageBoxButtons.OK);
            }

            return status;
        }

        private bool checkTransactionReceiptDetailsFields()
        {
            bool status = true;

            if (!ErrorUtilities.ShowErrorMessages(this.comboBoxDonationAccount.SelectedIndex > 0,
                this.labelErrorDonationAccount, string.Format(Resources.SikraResource.WrongSelectionErrorMsg, "a", "Donation account.")) ||
                string.IsNullOrEmpty(this.textBoxReceiptNumber.Text))
            {
                status = false;
            }

            if (!status)
            {
                MessageBox.Show("Enter correct Receipt details.", "Ok", MessageBoxButtons.OK);
            }

            return status;
        }

        private void clearErrorTexts(bool personDetailsErrors, bool modeDetailsErrors, bool receiptDetailsErrors)
        {
            if (personDetailsErrors)
            {
                this.labelErrorFirstName.Text = "";
                this.labelErrorLastName.Text = "";
                this.labelErrorAddressLine1.Text = "";
                this.labelErrorEmailAddress.Text = "";
            }
            if (modeDetailsErrors)
            {
                this.labelErrorModeNumber.Text = "";
                this.labelErrorBankName.Text = "";
                this.labelErrorAmount.Text = "";
            }
            if (receiptDetailsErrors)
            {
                this.labelErrorDonationAccount.Text = "";
                this.labelErrorReceiptNumber.Text = "";
            }
        }

        private void clearTexts(bool personDetails, bool modeDetails, bool receiptDetails)
        {
            if (personDetails)
            {
                this.textBoxLastName.Text = "";
                this.textBoxAddressLine1.Text = "";
                this.textBoxAddressLine2.Text = "";
                this.textBoxAddressLine3.Text = "";
                this.textBoxAddressLine4.Text = "";
                this.textBoxPhoneNumber.Text = "";
                this.textBoxEmailAddress.Text = "";
            }
            if (modeDetails)
            {
                this.textBoxModeNumber.Text = "";
                this.textBoxTransactionModeBankName.Text = "";
                this.textBoxTransactionAmount.Text = "";
                this.comboBoxPaymentMode.SelectedIndex = 0;
            }
            if (receiptDetails)
            {
                this.textBoxReceiptNumber.Text = "";
                this.comboBoxDonationAccount.Items.Clear();
                this.comboBoxPurpose.SelectedIndex = 0;
            }
        }

        private void makeModeGridVisible(string mode, bool enabledOrDisabled)
        {
            makePurposeGridVisibleOrInvisible(enabledOrDisabled);

            labelTransactionDate.Enabled = enabledOrDisabled;
            dateTimePickerTransactionDate.Enabled = enabledOrDisabled;
            labelRequiredModeDate.Enabled = enabledOrDisabled;

            labelModeNumber.Text = mode + " Number";
            labelModeNumber.Enabled = enabledOrDisabled;
            labelRequiredModeNumber.Enabled = enabledOrDisabled;
            textBoxModeNumber.Enabled = enabledOrDisabled;

            labelTransactionModeDate.Text = mode + " Date";
            labelTransactionModeDate.Enabled = enabledOrDisabled;
            labelRequiredTransactionModeDate.Enabled = enabledOrDisabled;
            dateTimePickerTransactionModeDate.Enabled = enabledOrDisabled;

            labelTransactionModeBankName.Enabled = enabledOrDisabled;
            labelRequiredTransactionModeBankName.Enabled = enabledOrDisabled;
            textBoxTransactionModeBankName.Enabled = enabledOrDisabled;

            labelTransactionAmount.Enabled = enabledOrDisabled;
            labelRequiredTransactionAmount.Enabled = enabledOrDisabled;
            textBoxTransactionAmount.Enabled = enabledOrDisabled;
        }

        private void makePurposeGridVisibleOrInvisible(bool visibleOrNot)
        {
            comboBoxPurpose.Enabled = visibleOrNot;

            labelReceiptDate.Enabled = visibleOrNot;
            dateTimePickerReceiptDate.Enabled = visibleOrNot;

            labelReceiptNumber.Enabled = visibleOrNot;
            textBoxReceiptNumber.Enabled = visibleOrNot;

            labelSentBy.Enabled = visibleOrNot;
            comboBoxSentByType.Enabled = visibleOrNot;

            labelDespatchDate.Enabled = visibleOrNot;
            dateTimePickerDespatchDate.Enabled = visibleOrNot;
        }

        private void populatePersonDetailsWithExistingUser()
        {
            this.textBoxTitle.Text = _currentPerson.Title;
            this.comboBoxFirstName.Text = _currentPerson.FirstName;
            this.textBoxLastName.Text = _currentPerson.LastName;
            this.dateTimePickerDate.Value = _currentPerson.EntryDate;

            this.textBoxAddressLine1.Text = _currentAddress.AddressLine1;
            this.textBoxAddressLine2.Text = _currentAddress.AddressLine2;
            this.textBoxAddressLine3.Text = _currentAddress.AddressLine3;
            this.textBoxAddressLine4.Text = _currentAddress.AddressLine4;
            this.textBoxPhoneNumber.Text = _currentAddress.PhoneNumber;
            this.textBoxEmailAddress.Text = _currentAddress.Email;
        }

        private void populateTransactionDetailsWithExisting()
        {
            bool isFirst = true;
            TransactionModeNames toBeSelected = null;
            foreach (var v in this.comboBoxPaymentMode.Items)
            {
                if (isFirst)
                {
                    isFirst = false;
                    continue;
                }
                if ((v as TransactionModeNames).ModeID != _currentTransaction.TransactionModeID)
                    continue;
                else
                {
                    toBeSelected = v as TransactionModeNames;
                    break;
                }
            }
            this.comboBoxPaymentMode.SelectedItem = toBeSelected;
            this.textBoxModeNumber.Text = _currentTransaction.TransactionModeNumber;
            this.dateTimePickerTransactionDate.Value = _currentTransaction.TransactionDate;
            this.dateTimePickerTransactionModeDate.Value = _currentTransaction.TransactionModeDate.Value;
            this.textBoxTransactionModeBankName.Text = _currentTransaction.TransactionModeBank;
            this.textBoxTransactionAmount.Text = _currentTransaction.TransactionAmount.ToString();
        }

        private void populateTransactionReceiptDetailsWithExisting()
        {
            bool isFirst = true;
            DonationPlaceNames donationPlaceNameToBeSelected = null;
            PostalModeNames postalModeToBeSelected = null;

            foreach (var v in this.comboBoxPurpose.Items)
            {
                if (isFirst)
                {
                    isFirst = false;
                    continue;
                }
                if ((v as DonationPlaceNames).DonationPlaceID != _currentTransactionReceiptDetail.DonationPlaceID)
                    continue;
                else
                {
                    donationPlaceNameToBeSelected = v as DonationPlaceNames;
                    break;
                }
            }
            isFirst = true;
            foreach (var v in this.comboBoxSentByType.Items)
            {
                if (isFirst)
                {
                    isFirst = false;
                    continue;
                }
                if ((v as PostalModeNames).PostalModeID != _currentTransactionReceiptDetail.PostalModeID)
                    continue;
                else
                {
                    postalModeToBeSelected = v as PostalModeNames;
                    break;
                }
            }
            this.comboBoxPurpose.SelectedItem = donationPlaceNameToBeSelected;
            this.comboBoxSentByType.SelectedItem = postalModeToBeSelected;
            this.textBoxReceiptNumber.Text = _currentTransactionReceiptDetail.TransactionReceiptNumber;
            this.dateTimePickerReceiptDate.Value = _currentTransactionReceiptDetail.TransactionReceiptDate;
            this.dateTimePickerDespatchDate.Value = _currentTransactionReceiptDetail.DespatchDate.Value;
        }

        private void makeDonationAccountComboBoxSelection()
        {
            bool isFirst = true;
            DonationAccountNames donationAccountNameToBeSelected = null;

            foreach (var v in this.comboBoxDonationAccount.Items)
            {
                if (isFirst)
                {
                    isFirst = false;
                    continue;
                }
                if ((v as DonationAccountNames).DonationAccountID != _currentTransactionReceiptDetail.DonationAccountID)
                    continue;
                else
                {
                    donationAccountNameToBeSelected = v as DonationAccountNames;
                    break;
                }
            }
            this.comboBoxDonationAccount.SelectedItem = donationAccountNameToBeSelected;
        }

        # endregion

    }
}
